import { MockMethod } from 'vite-plugin-mock'
import pkg from 'mockjs'
const { Random } = pkg

// 数据库列表数据
const databaseList = [
  {
    id: '1',
    name: 'MySQL生产环境',
    type: 'mysql',
    host: 'mysql.example.com',
    port: 3306,
    username: 'admin',
    password: '********',
    database: 'production_db',
    description: '生产环境主数据库，存储核心业务数据',
    logo: 'https://via.placeholder.com/64?text=MySQL',
    createdAt: '2023-09-01',
    updatedAt: '2023-09-01'
  },
  {
    id: '2',
    name: 'PostgreSQL分析库',
    type: 'postgresql',
    host: 'postgres.example.com',
    port: 5432,
    username: 'analyst',
    password: '********',
    database: 'analytics_db',
    description: '数据分析专用数据库，存储分析结果和报表数据',
    logo: 'https://via.placeholder.com/64?text=PG',
    createdAt: '2023-09-05',
    updatedAt: '2023-09-05'
  },
  {
    id: '3',
    name: 'MongoDB文档存储',
    type: 'mongodb',
    host: 'mongo.example.com',
    port: 27017,
    username: 'mongouser',
    password: '********',
    database: 'documents',
    description: '用于存储非结构化文档数据',
    logo: 'https://via.placeholder.com/64?text=Mongo',
    createdAt: '2023-09-10',
    updatedAt: '2023-09-10'
  },
  {
    id: '4',
    name: 'Redis缓存服务',
    type: 'redis',
    host: 'redis.example.com',
    port: 6379,
    username: 'redisuser',
    password: '********',
    database: '0',
    description: '高性能缓存服务，提升应用响应速度',
    logo: 'https://via.placeholder.com/64?text=Redis',
    createdAt: '2023-09-15',
    updatedAt: '2023-09-15'
  }
]

// 数据库类型对应的logo
const logoMap = {
  mysql: 'https://via.placeholder.com/64?text=MySQL',
  postgresql: 'https://via.placeholder.com/64?text=PG',
  mongodb: 'https://via.placeholder.com/64?text=Mongo',
  oracle: 'https://via.placeholder.com/64?text=Oracle',
  sqlserver: 'https://via.placeholder.com/64?text=MSSQL',
  sqlite: 'https://via.placeholder.com/64?text=SQLite',
  redis: 'https://via.placeholder.com/64?text=Redis'
}

export default [
  // 获取数据库列表
  {
    url: '/api/database/list',
    method: 'get',
    response: () => {
      return {
        code: 0,
        data: databaseList,
        message: '获取成功'
      }
    }
  },
  
  // 获取数据库详情
  {
    url: '/api/database/:id',
    method: 'get',
    response: (request) => {
      const { id } = request.params
      const database = databaseList.find(item => item.id === id)
      
      if (database) {
        return {
          code: 0,
          data: database,
          message: '获取成功'
        }
      } else {
        return {
          code: 1,
          data: null,
          message: '数据库不存在'
        }
      }
    }
  },
  
  // 添加数据库
  {
    url: '/api/database/add',
    method: 'post',
    response: (request) => {
      const database = request.body
      const newDatabase = {
        id: Random.id(),
        ...database,
        logo: logoMap[database.type] || 'https://via.placeholder.com/64?text=DB',
        createdAt: new Date().toISOString().split('T')[0],
        updatedAt: new Date().toISOString().split('T')[0]
      }
      
      databaseList.unshift(newDatabase)
      
      return {
        code: 0,
        data: newDatabase,
        message: '添加成功'
      }
    }
  },
  
  // 更新数据库
  {
    url: '/api/database/:id',
    method: 'put',
    response: (request) => {
      const { id } = request.params
      const updateData = request.body
      
      const index = databaseList.findIndex(item => item.id === id)
      if (index !== -1) {
        // 如果类型改变，更新logo
        if (updateData.type && updateData.type !== databaseList[index].type) {
          updateData.logo = logoMap[updateData.type] || 'https://via.placeholder.com/64?text=DB'
        }
        
        databaseList[index] = {
          ...databaseList[index],
          ...updateData,
          updatedAt: new Date().toISOString().split('T')[0]
        }
        
        return {
          code: 0,
          data: databaseList[index],
          message: '更新成功'
        }
      } else {
        return {
          code: 1,
          data: null,
          message: '数据库不存在'
        }
      }
    }
  },
  
  // 删除数据库
  {
    url: '/api/database/:id',
    method: 'delete',
    response: (request) => {
      const { id } = request.params
      
      const index = databaseList.findIndex(item => item.id === id)
      if (index !== -1) {
        databaseList.splice(index, 1)
        
        return {
          code: 0,
          data: null,
          message: '删除成功'
        }
      } else {
        return {
          code: 1,
          data: null,
          message: '数据库不存在'
        }
      }
    }
  },
  
  // 测试数据库连接
  {
    url: '/api/database/test-connection',
    method: 'post',
    response: (request) => {
      const data = request.body
      
      // 模拟连接测试，随机成功或失败
      const isSuccess = Math.random() > 0.2 // 80%概率成功
      
      if (isSuccess) {
        return {
          code: 0,
          data: {
            connected: true,
            message: '连接成功'
          },
          message: '测试成功'
        }
      } else {
        return {
          code: 1,
          data: {
            connected: false,
            message: '连接失败: 无法连接到数据库服务器'
          },
          message: '测试失败'
        }
      }
    }
  }
] as MockMethod[] 